"""
从VOC数据集中分离出自己想要的图片类别
这里我选择了自己喜欢的5个类别
"""

import os
import xml.etree.ElementTree as ET
import shutil

ann_filepath = 'E:/KD/VOC/VOCdevkit/VOC2007/Annotations/'
img_filepath = 'E:/KD/VOC/VOCdevkit/VOC2007/JPEGImages/'
img_savepath = 'E:/KD/VOC/JPEGImages/'
ann_savepath = 'E:/KD/VOC/Annotations/'
if not os.path.exists(img_savepath):
    os.mkdir(img_savepath)

if not os.path.exists(ann_savepath):
    os.mkdir(ann_savepath)

classes = ['cat', 'cow', 'sheep', 'dog', 'horse']


def save_annotation(file):
    tree = ET.parse(ann_filepath + '/' + file)
    root = tree.getroot()
    result = root.findall("object")
    bool_num = 0
    for obj in result:
        if obj.find("name").text not in classes:
            root.remove(obj)
        else:
            bool_num = 1
    if bool_num:
        tree.write(ann_savepath + file)
        return True
    else:
        return False


def save_images(file):
    name_img = img_filepath + os.path.splitext(file)[0] + ".jpg"
    shutil.copy(name_img, img_savepath)
    return True


if __name__ == '__main__':
    for f in os.listdir(ann_filepath):
        if save_annotation(f):
            save_images(f)
